import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/pages/common/report/studentReportBottom.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/studentsReportDataGrid.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';

// ignore: must_be_immutable
class TeacherReportsStudent extends StatefulWidget {
  TeacherReportsStudent({Key? key, required this.showAppbar}) : super(key: key);
  bool showAppbar;
  static const String routeName = '/teacher/reports/student';

  @override
  _TeacherReportsStudent createState() => _TeacherReportsStudent();
}

class _TeacherReportsStudent extends State<TeacherReportsStudent> {
  final ReportsController _reportsController = Get.put(ReportsController());
  var box = Hive.box('appconfig');
  String classId = '';
  var selectedTime = Constants.timeRanges[0];
  var startTime;
  var endTime;

  @override
  void initState() {
    classId = box.get(CacheManagerKey['currentclass'].toString());

    if (_reportsController.classoverviewdetailsresult.value.result != null &&
        classId != '' &&
        _reportsController.classoverviewdetailsresult.value.result != null &&
        _reportsController.classoverviewdetailsresult.value.result?.dataList !=
            null &&
        _reportsController
            .classoverviewdetailsresult.value.result!.dataList!.isNotEmpty) {
      _reportsController.classoverviewdetailsresult.value.result!.dataList!
          .clear();
    }
    Future.delayed(Duration(milliseconds: 100), () {
      fetchOverallData(classId, startTime, endTime);
    });
    super.initState();
  }

  void fetchOverallData(classId, startTime, endTime) {
    _reportsController.fetchClassOverviewDetails(classId, startTime, endTime);
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher report Student');

    return Scaffold(
      backgroundColor: Colors.transparent,
       appBar: widget.showAppbar ? CustomAppBar(title: 'Student Report'.tr) : null,
      body: Column(
        children: [
          StudentReportBottom(
            classId: classId,
            selectedTime: selectedTime,
            updateSelectedTime: (arg) {
              setState(() {
                selectedTime = arg;
              });
            },
            startTime: startTime,
            endTime: endTime,
            updateCustomTime: (arg1, arg2) {
              setState(() {
                startTime = arg1;
                endTime = arg2;
              });
              fetchOverallData(classId, arg1, arg2);
            },
          ),
          Expanded(child: _reportsController.obx((data) {
            if (_reportsController.classoverviewdetailsresult.value.result != null &&
                classId != '' &&
                _reportsController.classoverviewdetailsresult.value.result !=
                    null &&
                _reportsController
                        .classoverviewdetailsresult.value.result?.dataList !=
                    null &&
                _reportsController.classoverviewdetailsresult.value.result!
                    .dataList!.isNotEmpty) {
              return Padding(
                padding: const EdgeInsets.only(top: 12.0),
                child: StudentsReportDataGrid(
                    studentOverviewItems: _reportsController
                        .classoverviewdetailsresult.value.result!.dataList!,
                    classId: classId),
              );
            } else {
              return Center(child: Text(''));
            }
          }))
        ],
      ),
    );
  }
}
